


// IV Attrition analysis // 




use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd  G:\Data\Workdata\707677\Common\03Output\02Regresssions





keep if time==1

tab obs_lawyer

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court*obs_court-granted)/(obs_court-1)

* Data restrictions more than 10 obs pr lawyer and multiple lawyers in a given court*year



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique



merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"



keep if _merge==3
drop _merge

keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)

egen court_pyear =group(court pyear) 
tab court_pyear

tab time

tab aar, gen(aardum)



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */

// complier means for non-granted
gen no_treat=(1-granted)
gen y_no_treat= no_treat*attrition
gen y_treat= granted*attrition
ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

sum attrition if granted==0
local dismissed_mean=r(mean)

sum attrition 
local dependent_mean=r(mean)


reg   attrition granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)


local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



ivreghdfe attrition (granted=iv_lawyer2) `Xvar_short', absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_attrition.xls, replace ctitle(IV attrition full time) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Cumulative 


ivreghdfe y_no_treat (no_treat=iv_lawyer2) if time==6, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

sum attrition if granted==0 & time==6
local dismissed_mean=r(mean)

sum attrition if time==6
local dependent_mean=r(mean)


reg   attrition granted `Xvar_short' i.court_pyear if time==6
local individuals=e(N)

ivreghdfe attrition (granted=iv_lawyer2) `Xvar_short'  if time==6, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_attrition.xls, append ctitle(IV attrition time 6) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', dependent_mean, `dependent_mean', individuals, `individuals')



 ivreghdfe y_no_treat (no_treat=iv_lawyer2) if time==11, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

sum attrition if granted==0 & time==11
local dismissed_mean=r(mean)

sum attrition if time==11
local dependent_mean=r(mean)



reg   attrition granted `Xvar_short' i.court_pyear if time==11
local individuals=e(N)


ivreghdfe attrition (granted=iv_lawyer2) `Xvar_short' if time==11, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_attrition.xls, append ctitle(IV attrition time 11) keep(granted)  ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', dependent_mean, `dependent_mean', individuals, `individuals')


ivreghdfe y_no_treat (no_treat=iv_lawyer2) if time==16, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

sum attrition if granted==0 & time==16
local dismissed_mean=r(mean)

sum attrition if time==16
local dependent_mean=r(mean)



reg   attrition granted `Xvar_short' i.court_pyear if time==16
local individuals=e(N)

ivreghdfe attrition (granted=iv_lawyer2) `Xvar_short' if time==16, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_attrition.xls, append ctitle(IV attrition time 16) keep(granted)  ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Run results on balanced sample  

bysort pnr: egen attrited= sum(attrition) // attrited==0 mean we have full period for the individual

keep if attrited==0




local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"




/* IV Regress */

sum erhvervsindk_real if granted==0


local dismissed_mean=r(mean)


reg   erhvervsindk_real granted i.court_pyear if time==1


local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2)  `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_results_balanced_panel.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
addstat(Non-granted mean, `dismissed_mean', individuals, `individuals')




local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social mortgage_real"




foreach var of varlist `depvar'{

sum `var' if granted==0
local dismissed_mean=r(mean)

reg `var' granted i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_balanced_panel.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean', individuals, `individuals')





}




sum bankgaeld_real if granted==0
local dismissed_mean=r(mean)

reg bankgaeld_real granted i.court_pyear if time==1
local individuals=e(N)

ivreghdfe bankgaeld_real (granted=iv_lawyer2) `Xvar_short' aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_balanced_panel.xls, append ctitle(IV bankgaeld_real)  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean', individuals, `individuals')













